﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jalal.Library.Data;
using System.Web;
using System.Web.Caching;
using System.Data;
using BCGC.DAL;
using Jalal.Library.Utility;
using Jalal.Library.Mail;

namespace BCGC.BLL.BusinessObject
{
    public class Settings
    {
        private int id;
        public int Id
        {
            get{ return id;}
        }

        private string adminUsername;
        public string AdminUsername
        {
            get{ return adminUsername;}
            set{ adminUsername = value;}
        }

        private string adminPassword;
        public string AdminPassword
        {
            set{ adminPassword = Security.Encrypt(value, this.encryptionKey, true);}
        }

        private string adminEmail;
        public string AdminEmail
        {
            get{ return adminEmail;}
            set{ adminEmail = value;}
        }

        private string supportEmail;
        public string SupportEmail
        {
            get{ return supportEmail;}
            set{ supportEmail = value;}
        }

	    private string mailServer;
        public string MailServer
        {
            get{ return mailServer;}
            set{ mailServer = value;}
        }

        private string mailUsername;
        public string MailUsername
        {
            get{ return mailUsername;}
            set{ mailUsername = value;}
        }

        private string mailPassword;
        public string MailPassword
        {
            get { return mailPassword; }
            set{ mailPassword = Security.Encrypt(value, this.encryptionKey,true);}
        }

        private int mailPort; 
	    public int MailPort
        {
            get{ return mailPort;}
            set{ mailPort = value;}
        }
	
	    private bool useSSLMail;
	    public bool UseSSLMail
        {
            get{ return useSSLMail;}
            set{ useSSLMail = value;}
        }
	
	    private string encryptionKey;
        public string EncryptionKey
        {
            get{ return encryptionKey;}
            set{ encryptionKey = value;}
        }

        private string siteName;
        public string SiteName
        {
            get{ return siteName;}
            set{ siteName = value;}
        }

        private string siteUrl;
        public string SiteUrl
        {
            get{ return siteUrl;}
            set{ siteUrl = value;}
        }

        private string companyEmail;
        public string CompanyEmail
        {
            get{ return companyEmail ;}
            set{ companyEmail = value;}
        }

        private string companySiteUrl;
        public string CompanySiteUrl
        {
            get { return companySiteUrl; }
            set { companySiteUrl = value; }
        }

        public Settings()
        {
            this.id = 0;
            this.adminUsername = string.Empty;
            this.adminPassword = string.Empty;
            this.adminEmail = string.Empty;
            this.companyEmail = string.Empty;
            this.companySiteUrl = string.Empty;
            this.encryptionKey = string.Empty;
            this.mailPassword = string.Empty;
            this.mailPort = 0;
            this.mailServer = string.Empty;
            this.mailUsername = string.Empty;
            this.siteName = string.Empty;
            this.siteUrl = string.Empty;
            this.supportEmail = string.Empty;
            this.useSSLMail = false;
        }

        public Settings(DataRow dataRow)
        {
            if (dataRow != null)
            {
                if (dataRow.Table.Columns.Contains("Id"))
                    this.id = (int)CommonUtility.FilterNull(dataRow["Id"], typeof(int));
                if (dataRow.Table.Columns.Contains("AdminUsername"))
                    this.adminUsername = (string)CommonUtility.FilterNull(dataRow["AdminUsername"], typeof(string));

                this.adminPassword = string.Empty;
                
                if (dataRow.Table.Columns.Contains("AdminEmail"))
                    this.adminEmail = (string)CommonUtility.FilterNull(dataRow["AdminEmail"], typeof(string));
                if (dataRow.Table.Columns.Contains("CompanyEmail"))
                    this.companyEmail = (string)CommonUtility.FilterNull(dataRow["CompanyEmail"], typeof(string));
                if (dataRow.Table.Columns.Contains("CompanySiteUrl"))
                    this.companySiteUrl = (string)CommonUtility.FilterNull(dataRow["CompanySiteUrl"], typeof(string));
                if (dataRow.Table.Columns.Contains("EncryptionKey"))
                    this.encryptionKey = (string)CommonUtility.FilterNull(dataRow["EncryptionKey"], typeof(string));
                if (dataRow.Table.Columns.Contains("MailPassword"))
                    this.mailPassword = (string)CommonUtility.FilterNull(dataRow["MailPassword"], typeof(string));
                if (dataRow.Table.Columns.Contains("MailPort"))
                    this.mailPort = (int)CommonUtility.FilterNull(dataRow["MailPort"], typeof(int));
                if (dataRow.Table.Columns.Contains("MailServer"))
                    this.mailServer = (string)CommonUtility.FilterNull(dataRow["MailServer"], typeof(string));
                if (dataRow.Table.Columns.Contains("MailUsername"))
                    this.mailUsername = (string)CommonUtility.FilterNull(dataRow["MailUsername"], typeof(string));
                if (dataRow.Table.Columns.Contains("SiteName"))
                    this.siteName = (string)CommonUtility.FilterNull(dataRow["SiteName"], typeof(string));
                if (dataRow.Table.Columns.Contains("SiteUrl"))
                    this.siteUrl = (string)CommonUtility.FilterNull(dataRow["SiteUrl"], typeof(string));
                if (dataRow.Table.Columns.Contains("SupportEmail"))
                    this.supportEmail = (string)CommonUtility.FilterNull(dataRow["SupportEmail"], typeof(string));
                if (dataRow.Table.Columns.Contains("UseSSLMail"))
                    this.useSSLMail = (bool)CommonUtility.FilterNull(dataRow["UseSSLMail"], typeof(bool));
            }
            else
            {
                this.id = 0;
                this.adminUsername = string.Empty;
                this.adminPassword = string.Empty;
                this.adminEmail = string.Empty;
                this.companyEmail = string.Empty;
                this.companySiteUrl = string.Empty;
                this.encryptionKey = string.Empty;
                this.mailPassword = string.Empty;
                this.mailPort = 0;
                this.mailServer = string.Empty;
                this.mailUsername = string.Empty;
                this.siteName = string.Empty;
                this.siteUrl = string.Empty;
                this.supportEmail = string.Empty;
                this.useSSLMail = false;
            }
        }

        public static Settings SiteSettings
        {
            get
            {
                Settings settings = (Settings)HttpContext.Current.Cache["Settings"];

                if (settings == null)
                {
                    DataFactory.GetDataMiner().BeginTransaction(true);

                    DataTable data = SettingsData.GetSettingsData();
                    if (data.Rows.Count > 0)
                        settings = new Settings(data.Rows[0]);

                    DataFactory.GetDataMiner().CommitTransaction();

                    HttpContext.Current.Cache.Insert("Settings", settings, null, Cache.NoAbsoluteExpiration,
                        Cache.NoSlidingExpiration);
                }

                return settings;
            }
        }

        public static MailSettings MailSettings
        {
            get
            {
                MailSettings mailSettings = new MailSettings();
                Settings settings = Settings.SiteSettings;

                if (settings != null)
                {
                    mailSettings.AdminMail = settings.AdminEmail;
                    mailSettings.SupportMail = settings.SupportEmail;
                    mailSettings.UserName = settings.MailUsername;
                    mailSettings.Password = settings.MailPassword;
                    mailSettings.Port = settings.MailPort;
                    mailSettings.UseSSL = settings.UseSSLMail;
                    mailSettings.ServerAddress = settings.MailServer;
                }

                return mailSettings;
            }
        }

        public bool Update()
        {
            DataFactory.GetDataMiner().BeginTransaction(true);

            int count = SettingsData.UpdateSettingsData(this.adminUsername, this.adminPassword, this.adminEmail,
                this.supportEmail, this.mailServer, this.mailUsername, this.mailPassword,
                this.mailPort, this.useSSLMail, this.encryptionKey, this.siteName, this.siteUrl,
                this.companyEmail, this.companySiteUrl);

            DataFactory.GetDataMiner().CommitTransaction();

            return (count == 1);
        }

        public static bool AdminPasswordMatch(string adminPassword)
        {
            DataFactory.GetDataMiner().BeginTransaction(true);

            int count = SettingsData.CountAdminPasswordMatch(adminPassword);

            DataFactory.GetDataMiner().CommitTransaction();

            return (count == 1);
        }
    }
}
